/*
** prend_pose.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:35:43 2011 michael guidet
** Last update Mon Jul 11 10:14:41 2011 michael guidet
*/

#include "server.h"

int	object_id(char *tab)
{
  int	idobjet;

  idobjet = -1;
  if (strcmp(tab, "nourriture") == 0)
    idobjet = NOURRITURE;
  else if (strcmp(tab, "linemate") == 0)
    idobjet = LINEMATE;
  else if (strcmp(tab, "deraumere") == 0)
    idobjet = DERAUMERE;
  else if (strcmp(tab, "sibur") == 0)
    idobjet = SIBUR;
  else if (strcmp(tab, "mendiane") == 0)
    idobjet = MENDIANE;
  else if (strcmp(tab, "phiras") == 0)
    idobjet = PHIRAS;
  else if (strcmp(tab, "thystame") == 0)
    idobjet = THYSTAME;
  return (idobjet);
}

void	graph_prend(t_fd *fd, t_env *e, int idobjet)
{
  char	cmd2[BUF_SIZE * 3];
  t_fd	*tmp;

  fd->pl->ressource[idobjet] += 1;
  if (idobjet == NOURRITURE)
    fd->pl->life += (long)(126.0f / (float)e->t * 1000000.0f);
  e->map[fd->pl->y][fd->pl->x].ressource[idobjet] -= 1;
  sprintf(cmd2, "pgt %d %d\n", fd->pl->id, idobjet);
  sprintf(cmd2, "%spin %d %d %d %d %d %d %d %d %d %d\n", cmd2, fd->pl->id,
	  fd->pl->x, fd->pl->y, fd->pl->ressource[NOURRITURE],
	  fd->pl->ressource[LINEMATE], fd->pl->ressource[DERAUMERE],
	  fd->pl->ressource[SIBUR], fd->pl->ressource[MENDIANE],
	  fd->pl->ressource[PHIRAS], fd->pl->ressource[THYSTAME]);
  sprintf(cmd2, "%sbct %d %d %d %d %d %d %d %d %d\n", cmd2, fd->pl->x,
	  fd->pl->y,
	  e->map[fd->pl->y][fd->pl->x].ressource[NOURRITURE],
	  e->map[fd->pl->y][fd->pl->x].ressource[LINEMATE],
	  e->map[fd->pl->y][fd->pl->x].ressource[DERAUMERE],
	  e->map[fd->pl->y][fd->pl->x].ressource[SIBUR],
	  e->map[fd->pl->y][fd->pl->x].ressource[MENDIANE],
	  e->map[fd->pl->y][fd->pl->x].ressource[PHIRAS],
	  e->map[fd->pl->y][fd->pl->x].ressource[THYSTAME]);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
}

void	graph_pose(t_fd *fd, t_env *e, int idobjet)
{
  char	cmd2[BUF_SIZE * 3];
  t_fd	*tmp;

  fd->pl->ressource[idobjet] -= 1;
  if (idobjet == NOURRITURE)
    fd->pl->life -= (long)(126.0f / (float)e->t * 1000000.0f);
  e->map[fd->pl->y][fd->pl->x].ressource[idobjet] += 1;
  sprintf(cmd2, "pdr %d %d\n", fd->pl->id, idobjet);
  sprintf(cmd2, "%spin %d %d %d %d %d %d %d %d %d %d\n", cmd2, fd->pl->id,
	  fd->pl->x, fd->pl->y, fd->pl->ressource[NOURRITURE],
	  fd->pl->ressource[LINEMATE], fd->pl->ressource[DERAUMERE],
	  fd->pl->ressource[SIBUR], fd->pl->ressource[MENDIANE],
	  fd->pl->ressource[PHIRAS], fd->pl->ressource[THYSTAME]);
  sprintf(cmd2, "%sbct %d %d %d %d %d %d %d %d %d\n", cmd2, fd->pl->x,
	  fd->pl->y,
	  e->map[fd->pl->y][fd->pl->x].ressource[NOURRITURE],
	  e->map[fd->pl->y][fd->pl->x].ressource[LINEMATE],
	  e->map[fd->pl->y][fd->pl->x].ressource[DERAUMERE],
	  e->map[fd->pl->y][fd->pl->x].ressource[SIBUR],
	  e->map[fd->pl->y][fd->pl->x].ressource[MENDIANE],
	  e->map[fd->pl->y][fd->pl->x].ressource[PHIRAS],
	  e->map[fd->pl->y][fd->pl->x].ressource[THYSTAME]);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
}

void	action_prendre(t_fd *fd, t_env *e, char *cmd)
{
  char	**tab;
  int	idobjet;

  printf("%sDrone %d %s Execute %s dans prend objet%s\n",
	 PURPLE, fd->pl->id, fd->pl->team, cmd, DEFAUT);
  fd->wait = -1;
  fd->idaction = -1;
  tab = str_to_wordtab(cmd, ' ');
  if (tab[1])
    {
      idobjet = object_id(tab[1]);
      if (idobjet != -1)
	if (e->map[fd->pl->y][fd->pl->x].ressource[idobjet] > 0)
	  {
	    graph_prend(fd, e, idobjet);
	    add_string_to_list(&fd->send_list, "ok\n");
	    free(tab[0]);
	    free(tab);
	    return ;
	  }
    }
  free(tab[0]);
  free(tab);
  add_string_to_list(&fd->send_list, "ko\n");
}

void	action_poser(t_fd *fd, t_env *e, char *cmd)
{
  char	**tab;
  int	idobjet;

  printf("%sDrone %d %s Execute %s dans pose objet%s\n",
	 PURPLE, fd->pl->id, fd->pl->team, cmd, DEFAUT);
  fd->wait = -1;
  fd->idaction = -1;
  tab = str_to_wordtab(cmd, ' ');
  if (tab[1])
    {
      idobjet = object_id(tab[1]);
      if (idobjet != -1)
	if (fd->pl->ressource[idobjet] > 0)
	  {
	    graph_pose(fd, e, idobjet);
	    add_string_to_list(&fd->send_list, "ok\n");
	    free(tab[0]);
	    free(tab);
	    return;
	  }
    }
  free(tab[0]);
  free(tab);
  add_string_to_list(&fd->send_list, "ko\n");
}
